package com.linghui.videoplus.ipai.api.db;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.linghui.videoplus.ipai.bean.MessageBean;

public class VideoPlusDB {

	private Activity mActivity;
	private Context mContext;
	private SQLiteDatabase db = null;

	public static final int DATABASE_VERSION = 2;
	private static final String DATABASE_NAME = "VideoPlus.db";
	public static final String T_MESSAGE = "T_MESSAGE";

	public VideoPlusDB(Activity activity) {
		this.mActivity = activity;
		create();
	}

	public VideoPlusDB(Context context) {
		this.mContext = context;
		create();
	}

	private void create() {
		if (db == null || !db.isOpen()) {
			if (mActivity != null) {
				db = mActivity.openOrCreateDatabase(DATABASE_NAME,
						Context.MODE_PRIVATE, null);
			} else if (mContext != null) {
				db = mContext.openOrCreateDatabase(DATABASE_NAME,
						Context.MODE_PRIVATE, null);
			}
		}
		db.execSQL("CREATE TABLE IF NOT EXISTS "
				+ T_MESSAGE
				+ " (id INTEGER UNIQUE, title VARCHAR, describe VARCHAR, createDate VARCHAR, is_read VARCHAR, is_replay VARCHAR);");

	}

	
	
	
	public long insertMessage(MessageBean bean) {
		return db.insert(VideoPlusDB.T_MESSAGE, null,
				messageToContentValues(bean));
	}
	
	public List<MessageBean> listMessage(){
		List<MessageBean> list=new ArrayList<MessageBean>();
		Cursor query=db.query(VideoPlusDB.T_MESSAGE, null, null, null, null, null, "id desc");
		if (query != null) {
			query.moveToFirst();
			while (!query.isAfterLast()) {
				MessageBean	bean =contentValuesTomessage(query);
				list.add(bean);
				query.moveToNext();
			}
		}
		query.close();
		return list;
	}
	
	
	private MessageBean contentValuesTomessage(Cursor c) {
		int columnId = c.getColumnIndex("id");
		int columnTitle = c.getColumnIndex("title");
		int columnDesc = c.getColumnIndex("describe");
		int columnCreateDate=c.getColumnIndex("createDate");
		int columnIsRead=c.getColumnIndex("is_read");
		int columnIsReplay=c.getColumnIndex("is_replay");
		
		MessageBean bean = new MessageBean();
		bean.setId(c.getString(columnId));
		bean.setTitle(c.getString(columnTitle));
		bean.setDescribe(c.getString(columnDesc));
		bean.setCreateDateStr(c.getString(columnCreateDate));
		bean.setIsRead(c.getString(columnIsRead));
		bean.setIsReplay(c.getString(columnIsReplay));
		
		return bean;
	}
	
	private ContentValues messageToContentValues(MessageBean bean) {
		final ContentValues v = new ContentValues();
		v.put("id", bean.getId());
		v.put("title", bean.getTitle());
		v.put("describe", bean.getDescribe());
		v.put("createDate", bean.getCreateDateStr());
		v.put("is_read", bean.getIsRead());
		v.put("is_replay", bean.getIsReplay());
		return v;
	}
	
	public void closeDb() {
		if (db.isOpen()) {
			db.close();
			db = null;
		}
	}

}
